package com.yyk.yykywd.mvvm.ui

import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.text.Editable
import android.text.TextWatcher
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.PopupWindow
import android.widget.TextView
import androidx.core.widget.PopupWindowCompat
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModel
import androidx.recyclerview.widget.LinearLayoutManager
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener
import com.yyk.yykywd.*
import com.yyk.yykywd.base.BaseActivity
import com.yyk.yykywd.mvvm.adapter.SearchAdapter
import com.yyk.yykywd.mvvm.view_model.SearchViewModel
import com.yyk.yykywd.util.ToAnotherActivity
import com.yyk.yykywd.util.ToastUtil
import kotlinx.android.synthetic.main.activity_search.*


class SearchActivity : BaseActivity() {
    private lateinit var popupWindow: PopupWindow
    private lateinit var searchAdapter: SearchAdapter
    private lateinit var searchViewModel: SearchViewModel

    override fun initData() {
    }

    override fun getLayoutId(): Int {
        return R.layout.activity_search
    }

    override fun initView() {
        searchViewModel = getViewModel(this, SearchViewModel::class.java)
        hidetoolBar()
        initPopupWindow()

        searchAdapter = SearchAdapter(mActivity,
                R.layout.item_agent,
                searchViewModel.searchs)
        searchAdapter.setType(AGENT)
        rv.layoutManager = LinearLayoutManager(mActivity)
        rv.adapter = searchAdapter

        tv_hint.visibility = View.GONE
        loading_layout.showContent()
    }

    private fun initPopupWindow() {
        val view = LayoutInflater.from(mActivity).inflate(R.layout.type_pop, null)
        popupWindow = PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        popupWindow.isOutsideTouchable = true
        popupWindow.isFocusable = true
        popupWindow.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        val tvHospital: TextView = view.findViewById(R.id.tv_hospital)
        val tvAgent: TextView = view.findViewById(R.id.tv_agent)
        tvHospital.setOnClickListener {
            tv_type.text = "医院"
            searchViewModel.type = HOSPITAL
            searchAdapter.setType(HOSPITAL)
            popupWindow.dismiss()
        }
        tvAgent.setOnClickListener {
            tv_type.text = "代理商"
            searchViewModel.type = AGENT
            searchAdapter.setType(AGENT)
            popupWindow.dismiss()
        }
    }

    override fun setListener() {
        super.setListener()

        tv_type.setOnClickListener {
            PopupWindowCompat.showAsDropDown(popupWindow, tv_type, 0, 0, Gravity.START)
        }

        iv_back.setOnClickListener {
            finish()
        }

        searchAdapter.setOnItemClickListener { _, _, position ->
            if (searchViewModel.searchs.isEmpty()) {
                return@setOnItemClickListener
            }
            if (searchViewModel.searchs[position].common_type != 1) {
                return@setOnItemClickListener
            }
            if (searchViewModel.type == AGENT) {
                ToAnotherActivity.toReturnMoneyDetailActivity(mActivity,
                        FROM_CUSTOMER_OPEN_SEA_AGENT,
                        searchViewModel.searchs[position].id.toString(), 0, 0, 0)
            } else {
                ToAnotherActivity.toReturnMoneyDetailActivity(mActivity,
                        FROM_CUSTOMER_OPEN_SEA_HOSPITAL,
                        searchViewModel.searchs[position].id.toString(), 0, 0, 0)
            }
        }

        with(searchViewModel) {
            mSearch.observe(this@SearchActivity, Observer {
                hideRefreshAndLoadMore(srl)
                searchAdapter.notifyDataSetChanged()
                if (it.code == 200) {
                    loading_layout.showContent()
                    tv_hint.visibility = View.GONE
                    return@Observer
                }
                if (searchs.size == 0) {
                    loading_layout.setEmptyTextColor(mActivity, R.color.black_33)
                    loading_layout.showEmpty(R.drawable.pic_wssdgkh, "未搜索到该客户")
                    tv_hint.visibility = View.VISIBLE
                } else {
                    loading_layout.showContent()
                    ToastUtil.showShort(it.msg)
                    tv_hint.visibility = View.GONE
                }
            })

            tv_search.setOnClickListener {
                research()
            }

            srl.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
                override fun onLoadMore(refreshLayout: RefreshLayout) {
                    searchViewModel.loadMore()
                    searchViewModel.search(getTextString(et_search))
                }

                override fun onRefresh(refreshLayout: RefreshLayout) {
                    research()
                }
            })
        }

    }

    private fun research() {
        val text = getTextString(et_search)
        if (text.isNotBlank()) {
            searchViewModel.refresh(searchViewModel.searchs)
            searchViewModel.search(text)
        } else {
            ToastUtil.showShort("请输入手机号或者姓名查询")
        }
    }
}
